<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="/plugins/select2/select2.css" />
	<link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
	<script src="/plugins/select2/select2.min.js" type="text/javascript"></script>
	<!--bootstrap-fileinput-->
	<link rel="stylesheet" type="text/css" href="/bootstrap-fileinput/css/fileinput.css">
	<script type="text/javascript" src="/bootstrap-fileinput/js/fileinput.js"></script>
	<script type="text/javascript" src="/bootstrap-fileinput/js/locales/zh.js"></script>
	<script>
		$(function () {
			getData();
			fileInput();
			// updatefileInput();
			$("#addCategoryId").select2();
			$("#updateCategoryId").select2();
		})

		function fileInput(){
			$('#aimg').fileinput({
				language: 'zh',//语言：中文
				uploadUrl: "/contentImg/upload", //上传地址
				browseClass: 'btn btn-primary', //按钮样式
				maxFileCount: 1,//最多支持上传的个数
				enctype: 'multipart/form-data',//序列化
				maxFileSize: 1024, //单位为kb, 如果为0表示不限制文件大小
				allowedFileExtensions: ['jpg', 'gif', 'png'],//限制上传文件的类型
			});
			$("#aimg").on("fileuploaded", function(event, data, previewId, index){
				if(data.response){
					$("#bimg").val(data.response.info)
				}
			})
		}

		function updatefileInput(){
			$('#uimg').fileinput({
				language: 'zh',//语言：中文
				uploadUrl: "/content/upload", //上传地址
				browseClass: 'btn btn-primary', //按钮样式
				maxFileCount: 1,//最多支持上传的个数
				enctype: 'multipart/form-data',//序列化
				maxFileSize: 1024, //单位为kb, 如果为0表示不限制文件大小
				allowedFileExtensions: ['jpg', 'gif', 'png'],//限制上传文件的类型
				initialPreviewAsData: true, // 是否初始化预览图片
				initialPreviewFileType: 'image', //回显文件的类型(初始化预览图片类型)
				initialPreview: ""+$("#dimg").val()+"" //图片地址
			});
			$("#uimg").on("fileuploaded", function(event, data, previewId, index){
				if(data.response){
					$("#dimg").val(data.response.data)
				}
			})
		}

		function getData() {
			$.ajax({
				url:"/content/listContent",
				type:"post",
				dataType:"json",
				success:function (result) {
					if(result.code == 200) {
						var data = result.data;
						var html = '';
						for (var i = 0; i < data.length; i++) {
							html += '<tr><td><input name="box" value="' + data[i].id + '" type="checkbox"></td>';
							html += '<td>' + data[i].id + '</td>';
							html += '<td>' + data[i].categoryId + '</td>';
							html += '<td>' + data[i].title + '</td>';
							html += '<td>' + data[i].url + '</td>';
							html += '<td>';
							html += '<img alt="" src="' + data[i].pic + '" width="100px" height="50px">';
							html += '</td>';
							html += '<td>' + data[i].sortOrder + '</td>';
							if (data[i].status == "1") {
								html += '<td>有效</td>';
							} else {
								html += '<td>无效</td>';
							}
							html += '<td class="text-center">';
							html += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="getContentById('+data[i].id+')">修改</button>';
							html += '</td>';
							html += '</tr>';
						}
						$("#tbody").html(html);
					}else{
						alert("查询失败");
					}
				},
				error:function () {
					alert("查询失败");
				}
			})
		}

		//添加操作
		function insert(){
			//广告分类
			var categoryData = $("#addCategoryId").select2("data");
			//获取id
			var categoryId = categoryData.id;
			var title = $("#addTitle").val();
			var url = $("#addUrl").val();
			var sortOrder = $("#addSortOrder").val();
			var pic = $("#bimg").val();
			var flag =  $("#addStatus").prop("checked");
			if(flag == true){
				$("#addStatus").attr("value","1");
			}else{
				$("#addStatus").attr("value","0");
			}
			var status = $("#addStatus").attr("value");
			$.ajax({
				url:"/content/insertContent",
				type:"post",
				dataType:"json",
				data:{categoryId : categoryId, title : title, url : url, sortOrder : sortOrder,
				pic : pic, status : status},
				success:function (result) {
					if(result.code == 200){
						alert("添加成功");
						location.reload();
					}else{
						alert("添加失败");
					}
				},
				error:function (result) {
					alert("添加失败");
				}
			})
		}

		//回显
		function getContentById(id) {
			$.ajax({
				url:"/content/getContentById",
				type:"post",
				dataType:"json",
				data:{id : id},
				success:function(result){
					if(result.code == 200){
						var data = result.data;
						$("#updateId").val(id);
						$("#updateCategoryId").val(data.categoryId).trigger("change");
						$("#updateTitle").val(data.title);
						$("#updateUrl").val(data.url);
						$("#dimg").val(data.pic);
						$("#updateSortOrder").val(data.sortOrder);
						if(data.status == "1"){
							$("#updateStatus").prop("checked",true);
						}else{
							$("#updateStatus").prop("checked",false);
						}
						updatefileInput();
					}else{
						alert("系统错误");
					}
				},
				error:function () {
					alert("系统错误");
				}
			})
		}

		function updateContentById(){
			var id = $("#updateId").val();
			//广告分类
			var categoryData = $("#updateCategoryId").select2("data");
			//获取categoryId
			var categoryId = categoryData.id;
			var title = $("#updateTitle").val();
			var url = $("#updateUrl").val();
			var sortOrder = $("#updateSortOrder").val();
			var pic = $("#dimg").val();
			var flag =  $("#updateStatus").prop("checked");
			if(flag == true){
				$("#updateStatus").attr("value","1");
			}else{
				$("#updateStatus").attr("value","0");
			}
			var status = $("#updateStatus").attr("value");
			$.ajax({
				url:"/content/updateContentById",
				type:"post",
				dataType:"json",
				data:{id : id,categoryId : categoryId, title : title, url : url,
					sortOrder : sortOrder, pic : pic, status : status},
				success:function (result) {
					if(result.code == 200){
						alert("修改成功");
						location.reload();
					}else{
						alert("修改失败");
					}
				},
				error:function (result) {
					alert("修改失败");
				}
			})
		}

		//删除
		function deleteContent() {
			var boxs = $("[name=box]:checked");
			var ids = [];
			$(boxs).each(function (i,e) {
				ids.push(e.value);
			})
			if(ids <= 0){
				alert.warning("请选择");
				return;
			}
			$.ajax({
				url:"/content/deleteContent",
				type:"post",
				dataType:"json",
				data:{ids : ids},
				success:function (result) {
					if(result.code == 200){
						alert("删除成功");
						location.reload();
					}else{
						alert("删除失败");
					}
				},
				error:function(result){
					alert("删除失败");
				}
			})
		}

	</script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteContent()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">

			                      </tbody>
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" id="addCategoryId" name="addCategoryId" style="width: 100%">
							<option th:each="category : ${contentCateList}" th:value="${category.id}">[[${category.name}]]</option>
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" placeholder="标题" id="addTitle">  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" placeholder="URL" id="addUrl">  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" placeholder="排序" id="addSortOrder">  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
									<label for="aimg">上传图片</label>
									<input type="file"class="form-control" name="myFile" id="aimg">
									<input type="text" name="bimg" id="bimg">
					            </td>
<!--								<td>-->
<!--									<img  src="{{entity.pic}}" width="200px" height="100px">-->
<!--								</td>-->
							</tr>						
						</table>
		      		</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input type="checkbox" class="icheckbox_square-blue" id="addStatus">
		      		</td>
		      	</tr>  	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 编辑窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">广告编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <input type="hidden" id="updateId">
					  </tr>
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <select class="form-control" id="updateCategoryId" name="updateCategoryId" style="width: 100%">
								  <option th:each="category : ${contentCateList}" th:value="${category.id}">[[${category.name}]]</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td><input  class="form-control" placeholder="标题" id="updateTitle">  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input  class="form-control" placeholder="URL" id="updateUrl">  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input  class="form-control" placeholder="排序" id="updateSortOrder">  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>
							  <table>
								  <tr>
									  <td>
										  <label for="uimg">上传图片</label>
										  <input type="file"class="form-control" name="myFile" id="uimg">
										  <input type="text" name="dimg" id="dimg">
									  </td>
									  <!--								<td>-->
									  <!--									<img  src="{{entity.pic}}" width="200px" height="100px">-->
									  <!--								</td>-->
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input type="checkbox" class="icheckbox_square-blue" id="updateStatus">
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateContentById()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>

</html>